package com.example.mapper;

import com.example.anno.AutoFill;
import com.example.enums.OperationType;
import com.example.pojo.Article;
import org.apache.ibatis.annotations.*;

import java.util.List;

@Mapper
public interface ArticleMapper {


    /**
     * 添加文章
     *
     * @param article 文章对象
     */
    @Insert("insert into article (title, content, cover_img, state, category_id, create_user, create_time, update_time) " +
            "values (#{title}, #{content}, #{coverImg}, #{state}, #{categoryId}, #{createUser}, #{createTime}, #{updateTime})")
    @AutoFill(OperationType.INSERT)
    void add(final Article article);

    /**
     * @param currentPage 当前页偏移量
     * @param pageSize    每条页数
     * @param userId      当前登录者的id
     * @param categoryId  文章分类ID 非必须
     * @param state       发布状态    已发布|草稿     非必须
     */
    List<Article> list(final Integer currentPage, final Integer pageSize,
                       final Integer userId,final Integer categoryId, final String state);


    /**
     * 根据主键id查询文章详情
     *
     * @param id 主键ID
     * @return 文章详情
     */
    @Select("select * from article where id = #{id}")
    Article findById(final Integer id);

    /**
     * 更新文章
     *
     * @param article 新的文章内容
     */
    @Update("update article " +
            "set title = #{title}, " +
            "content = #{content}, " +
            "cover_img = #{coverImg}, " +
            "state = #{state}, " +
            "category_id = #{categoryId}, " +
            "update_time = #{updateTime} " +
            "where id = #{id}")
    @AutoFill(OperationType.UPDATE)
    void update(final Article article);

    /**
     * 根据id删除文章
     *
     * @param id 文章id
     */
    @Delete("delete from article where id = #{id}")
    void delete(final Integer id);
}
